//
//  KFDMarketFinancialDataTableViewCell.m
//  
//
//  Created by JIRENTIANXIANG on 6/17/15.
//
//

#import "KFDMarketFinancialDatumTableViewCell.h"
#import "ui_config.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface KFDMarketFinancialDatumTableViewCell ()

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *formerValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *predicatedValueLabel;
@property (weak, nonatomic) IBOutlet UILabel *resultLabel;
@property (weak, nonatomic) IBOutlet UILabel *influenceLabel;
@property (strong,nonatomic) UIImageView* importanceBg;
@property (strong,nonatomic) UILabel* importanceLabel;
@property (strong,nonatomic) UIImageView* countryFlag;
@property (strong,nonatomic) UILabel* timeLabel;

@end

@implementation KFDMarketFinancialDatumTableViewCell

- (void)awakeFromNib {
    // Initialization code
    _importanceBg=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 18, 18)];
    _importanceBg.center=CGPointMake(SCREEN_WIDTH-20,15);
    _importanceLabel=[[UILabel alloc] initWithFrame:_importanceBg.bounds];
    _importanceLabel.textColor=[UIColor whiteColor];
    _importanceLabel.font=[UIFont systemFontOfSize:14];
    _importanceLabel.textAlignment=NSTextAlignmentCenter;
    [_importanceBg addSubview:_importanceLabel];
    [self addSubview:_importanceBg];
    
    _countryFlag=[[UIImageView alloc] initWithFrame:CGRectMake(10, 6, 28, 28)];
    _countryFlag.layer.cornerRadius=28/2;
    _countryFlag.layer.masksToBounds=YES;
    _countryFlag.contentMode=UIViewContentModeScaleAspectFill;
    [self addSubview:_countryFlag];
    
    _timeLabel=[[UILabel alloc] initWithFrame:CGRectMake(11, 36, 25, 10)];
    _timeLabel.font=[UIFont systemFontOfSize:8];
    _timeLabel.textAlignment=NSTextAlignmentCenter;
    _timeLabel.textColor=KFD_LIGHT_GRAY;
    [self addSubview:_timeLabel];
    
    self.influenceLabel.layer.cornerRadius=3;
    self.influenceLabel.layer.masksToBounds=YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(void)setFinancialDatum:(KFDFinancialDatum *)financialDatum
{
    self.titleLabel.text=financialDatum.content;
    self.formerValueLabel.text=[NSString stringWithFormat:@"前值 : %@",financialDatum.before_num ];
    self.predicatedValueLabel.text=[NSString stringWithFormat:@"预测 : %@",financialDatum.forecast];
    self.importanceLabel.text=financialDatum.important;
    [self.countryFlag sd_setImageWithURL:[NSURL URLWithString:financialDatum.country_img]];
    self.timeLabel.text=financialDatum.time;
    if ([financialDatum.important isEqualToString:@"高"]) {
        self.importanceBg.image=[UIImage imageNamed:@"boxstyle-red"];
        self.resultLabel.textColor=KFDColorRGB(0xb1, 0x2d, 0x35);
    }else if([financialDatum.important isEqualToString:@"中"]){
        self.importanceBg.image=[UIImage imageNamed:@"boxstyle-blue"];
        self.resultLabel.textColor=KFDColorRGB(0x47, 0xac, 0xf2);
    }else if([financialDatum.important isEqualToString:@"低"]){
        self.importanceBg.image=[UIImage imageNamed:@"boxstyle-green"];
        self.resultLabel.textColor=KFDColorRGB(0x59, 0xb4, 0x56);
    }
    self.resultLabel.text=[NSString stringWithFormat:@"结果 : %@",financialDatum.result];
    if ([financialDatum.result isEqualToString:@"待公布"]) {
        self.influenceLabel.hidden=YES;
    }else{
        float result=[financialDatum.result isEqualToString:@"持平"]?financialDatum.before_num.floatValue:financialDatum.result.floatValue;
        float forecast=financialDatum.forecast.floatValue;
        if (result>forecast) {
            self.influenceLabel.text=@"利多金银";
            self.influenceLabel.backgroundColor=KFDColorRGB(0xbd, 0x34, 0x2e);
        }else if (result<forecast){
            self.influenceLabel.text=@"利空金银";
            self.influenceLabel.backgroundColor=KFDColorRGB(0x00, 0x68, 0x56);
        }else{
            self.influenceLabel.text=@"影响较小";
            self.influenceLabel.backgroundColor=KFDColorRGB(0x8d, 0x8d, 0x8d);
        }
    }
}

-(void)drawRect:(CGRect)rect
{
    CGPoint origin=rect.origin;
    CGSize size=rect.size;
    CGContextRef context=UIGraphicsGetCurrentContext();
    UIColor* separatorColor=KFD_TABLEVIEW_SEPARATOR_COLOR;
    
    //separator
    CGContextSetStrokeColorWithColor(context, separatorColor.CGColor);
    CGContextSetLineWidth(context, 1);
    CGFloat left=origin.x+70;
    CGFloat right=origin.x+size.width-20;
    CGFloat y=origin.y+size.height;
    CGPoint separatorPoints[2]={
        CGPointMake(left, y),
        CGPointMake(right, y)
    };
    CGContextStrokeLineSegments(context, separatorPoints, 2);
    
    //vertical line
    CGContextSetStrokeColorWithColor(context, separatorColor.CGColor);
    CGFloat x=50;
    CGFloat top=origin.y;
    CGFloat bottom=origin.y+size.height;
    CGPoint verticalLinePoints[2]={
        CGPointMake(x, top),
        CGPointMake(x, bottom)
    };
    CGContextStrokeLineSegments(context, verticalLinePoints, 2);
    
    //dot
    CGContextSetFillColorWithColor(context, separatorColor.CGColor);
    CGContextFillEllipseInRect(context, CGRectMake(x-3, 15, 6, 6));
}

@end
